package cn.tedu.lucky.draw.server.controller;

import cn.tedu.lucky.draw.server.common.annotation.OperationLog;
import cn.tedu.lucky.draw.server.common.result.JsonResult;
import cn.tedu.lucky.draw.server.protrocol.po.Activity;
import cn.tedu.lucky.draw.server.protrocol.po.Prize;
import cn.tedu.lucky.draw.server.protrocol.query.ActivityQuery;
import cn.tedu.lucky.draw.server.service.IAtivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.batch.core.*;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin

@Api(tags="活动管理")
public class ActivityController {

    @Autowired
    private IAtivityService ativityService;

    @ApiOperation(value = "获取活动列表-管理端")
    @PostMapping("/draw/activity/getAll")
    public JsonResult<List<Activity>> getAll(@RequestBody ActivityQuery activityQuery){
        List<Activity> activityList=ativityService.getAll(activityQuery);
        return JsonResult.ok(activityList);
    }
    @ApiOperation(value = "新增活动-管理端")
    @PostMapping("/draw/activity/save")
    @OperationLog(operation = "新增活动")
    public JsonResult save(@RequestBody Activity activity){
        ativityService.save(activity);
        return JsonResult.ok();
    }
    @ApiOperation(value = "根据活动ID获取活动信息-管理端")
    @PostMapping("/draw/activity/{id}")
    public JsonResult<Activity> getActivtyById(@PathVariable Long id){
        Activity activity=ativityService.getActivtyById(id);
        return JsonResult.ok(activity);
    }

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job csvToDBJob;

    @Autowired
    private JobExplorer jobExplorer;


    @ApiOperation(value = "根据文件上传活动信息-管理端")
    @GetMapping("/activity/csvToDB")
    @OperationLog(operation = "上传活动")
    public String csvToDB() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        JobParameters jobParameters = new JobParametersBuilder(new JobParameters(), jobExplorer)
                .getNextJobParameters(csvToDBJob)
                .toJobParameters();
        JobExecution run = jobLauncher.run(csvToDBJob, jobParameters);
        return run.getId().toString();
    }

    @ApiOperation(value = "获取活动列表-App端")
    @PostMapping("/app/draw/activity/getAll")
    public JsonResult<List<Activity>> getAll(){
        List<Activity> activityList= ativityService.findAll();
        return JsonResult.ok(activityList);
    }


    @ApiOperation(value = "抽奖功能触发-App端")
    @GetMapping("/app/draw/activity/trigger")
    public JsonResult<Prize> Trigger(Long activityId) throws Exception{
        Prize prize=ativityService.trigger(activityId);
        return JsonResult.ok(prize);
    }
}
